<div class="layui-fluid layui-anim website-anim" id="template-add" lay-title="模板新增">
    <div class="layui-row layui-col-space8 website-container">
        <div class="layui-col-md6 layui-col-sm6 layui-col-xs12">
            <div class="layui-card">
                <div class="layui-card-body website-table-full">
                    <form class="layui-form layui-table-form" lay-filter="template-add-table-form">
                        <div class="layui-row">
                            <div class="layui-col-md8 layui-col-sm9 layui-col-xs9">
                            </div>
                            <div class="layui-col-md4 layui-col-sm12 layui-col-xs12 table-action-area">
                                <div class="layui-btn layui-btn-sm layui-btn-primary website-button-green-plain  table-action" id="reset">
                                    <i class="layui-icon">&#xe79b;</i>
                                </div>
                            </div>
                        </div>
                    </form>
                    <div class="eleTree menuTree" lay-filter="menuTree" style="margin-left: 1rem"></div>
                </div>
            </div>
        </div>
        <div class="layui-col-md6 layui-col-sm6 layui-col-xs12">
            <div class="layui-card">
                <div class="layui-card-header">新增模板</div>
                <div class="layui-card-body website-table-full">
                    <form class="layui-form layui-table-form" action="" lay-filter="template-add-form">
                        <div class="layui-form-item">
                            <label class="layui-form-label website-form-item-require">目录ID：</label>
                            <div class="layui-input-block">
                                <input type="text" name="menuId" lay-verify="required" disabled class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label website-form-item-require">目录名称：</label>
                            <div class="layui-input-block">
                                <input type="text" name="menuName" lay-verify="required" disabled autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label website-form-item-require">模板类型：</label>
                            <div class="layui-input-block">
                                <select name="templateTypeCode" lay-verify="required">
                                    <option value="">请选择</option>
                                    <option
                                            th:each="item : ${templateTypeCodes}"
                                            th:value="${item.code}" th:text="${item.name}">
                                    </option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label website-form-item-require">模板名称：</label>
                            <div class="layui-input-block">
                                <input type="text" name="templateName" lay-verify="required" autocomplete="off"
                                       class="layui-input" maxlength="50">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <input type="hidden" id="fileId" name="fileId" lay-verify="required" >
                            <label class="layui-form-label website-form-item-require">模板：</label>
                            <div class="layui-input-block">
                                <input type="text" disabled id="fileName" class="layui-input">
                            </div>
                            <button type="button" class="layui-btn" id="upload-file-update">
                                           <i class="layui-icon">&#xe67c;</i>上传模板
                            </button>
                        </div>
                        <button type="reset" class="layui-btn website-hide" id="reset-form"></button>
                        <button class="layui-btn website-hide" lay-submit="" lay-filter="template-form-submit"
                                id="submit"></button>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<script data-th-inline="none" type="text/javascript">
    layui.use(['jquery', 'laydate', 'website', 'form', 'eleTree', 'validate','upload'], function () {
        var $ = layui.jquery,
            laydate = layui.laydate,
            website = layui.website,
            form = layui.form,
            validate = layui.validate,
            eleTree = layui.eleTree,
            $view = $('#template-add'),
            $reset = $view.find('#reset'),
            $submit = $view.find('#submit'),
            upload = layui.upload,
            uploadFileEndApply,
            _currentMenuData,
            _menuTree;

        form.verify(validate);
        form.render();
        //目录选中时候的集合
        var selectArry= [];
        _menuTree = renderMenuTree();

        eleTree.on("nodeChecked(menuTree)", function (d) {
            //清空之前勾选所选的集合
            _menuTree.unCheckArrNodes(selectArry);
            //清空对象数组
            selectArry =[];
            var data = d.data.currentData.data;
            //把当前所选的数组放入集合记录
            if(d.isChecked){
                selectArry.push(data.menuId);
            }
            _currentMenuData = data;
            form.val("template-add-form", {
                "menuName": data.menuName,
                "menuId": data.menuId
            });
        });
        $reset.on('click', function () {
            $('#fileId').val("");
            reloadMenuTree();
            reset();
        });
        uploadFileEndApply = upload.render({
            elem: '#upload-file-update' //绑定元素
            , url: '/upload' //上传接口
            , done: function (res) {
                if (res.code === 200) {
                    var data = res.data
                    $('#fileName').val(data.name);
                    $('#fileId').val(data.fileId);
                    website.alert.success('上传文件成功');
                }
            }
            , error: function () {
                //请求异常回调
                website.alert.error('上传文件失败');
            },
            accept: 'file',
            size: 1024 * 50
        });
        $submit.on('click', function () {
            reloadMenuTree();
            $view.find('#submit-form').trigger('click');
        });

        function reset() {
            $view.find('#reset-form').trigger('click');
        }

        function renderMenuTree() {
            _menuTree = eleTree.render({
                elem: '.menuTree',
                url: ctx + 'menu/tree',
                where: {
                    "type":0,
                    "invalidate_ie_cache": new Date()
                },
                accordion: true,
                highlightCurrent: true,
                showCheckbox: true,
                checkStrictly: true,
                renderAfterExpand: false,
                request: {
                    name: "title",
                    key: "id",
                    children: "childs",
                    checked: "checked",
                    data: "data"
                },
                response: {
                    statusName: "code",
                    statusCode: 200,
                    dataName: "data"
                }
            });
            return _menuTree;
        }

        function reloadMenuTree() {
            _menuTree = renderMenuTree();
        }
        form.on('submit(template-form-submit)', function (data) {
            website.post(ctx + 'template', data.field, function () {
                layer.closeAll();
                website.alert.success('新增成功');
                $('#website-template').find('#query').click();
            })
            return false;
        });
    });
</script>